<?php

class Tags extends CActiveRecord {

    public $count;

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'Tags';
    }

    public function beforeValidate() {
        $this->name = trim(strip_tags($this->name));
        return true;
    }

    public static function addNewTags($str) {
        $tags = explode(",", $str);
        $ids = array();
        foreach($tags as $tag) {
            $tag = mb_strtolower(trim($tag), 'utf8');
            $tag = CHtml::encode($tag);
            $t = Tags::model()->find("name = ?", array($tag));
            if(!$t && !empty($tag)) {
                $t = new Tags();
                $t->name = $tag;
                $t->latin = YText::translit($tag);
                $t->save();
            }
            if($t){
                $ids[] = $t->id;
            }
        }
        return $ids;
    }
}
